﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using MSDN.Samples.ClaimsAuth;
using PictbaseAirApi;

namespace PictbaseSharePointLibrary
{
    public partial class PictbaseDataSync
    {
        private const String LISTS_WS = "/_vti_bin/lists.asmx";

        [DefaultValue(false)]
        public Boolean Claims { get; set; }

        public String SharePointSiteUrl { get; set; }
        public String SharePointList { get; set; }

        public String SharePointLogin { get; set; }
        public String SharePointAd { get; set; }

        public String SharePointPwd { get; set; }
        public String PictbaseUrl { get; set; }
        public String PictbaseLogin { get; set; }
        public String PictbasePwd { get; set; }
        public int PictbaseStructureId { get; set; }

        protected Structure _currentStructure = null;

        protected SharePoint.Lists.WS.Lists ListsWS = new SharePoint.Lists.WS.Lists();
        protected List<PictbaseAirApi.Field> _structureFields = null;
        protected XmlNode _list = null;

        protected List<Submission> submissionsToAdd = new List<Submission>();
        protected Dictionary<int, Submission> submissionsToUpdate = new Dictionary<int, Submission>();


        public void Init()
        {
            EntityManager.HttpServiceRoot = PictbaseUrl;
            EntityManager.HttpServiceLogin = PictbaseLogin;
            EntityManager.HttpServicePassword = PictbasePwd;

            ListsWS.Url = SharePointSiteUrl + LISTS_WS;
            if (Claims)
            {
                CookieCollection authCookie =
                ClaimClientContext.GetAuthenticatedCookies(SharePointSiteUrl, 925, 525);

                ListsWS.CookieContainer = new CookieContainer();
                ListsWS.CookieContainer.Add(authCookie);
            }
            else
            {
                ListsWS.Credentials = new System.Net.NetworkCredential(SharePointLogin, SharePointPwd, SharePointAd);
            }
        }

        protected String getSubmissionFieldValue(Submission submission, string fieldName)
        {
            switch (fieldName)
            {
                case "SubmissionId":
                    return submission.id.Value.ToString();
                case "latitude":
                    return submission.latitude.ToString();
                case "longitude":
                    return submission.longitude.ToString();
                case "structureId":
                    return submission.structureId.Value.ToString();
                case "structureName":
                    return submission.structureName;
                default:
                    Field field = (from f in _structureFields where f.systemName == fieldName select f).FirstOrDefault();
                    if (field == null)
                        return String.Empty;
                    if (!submission.Fields.ContainsKey(field.systemName))
                        return String.Empty;

                    switch (field.fieldType)
                    {
                        case "image":
                            return String.Format("{0}, {1}", submission.Fields[field.systemName], submission.id);
                        default:
                            return Convert.ToString(submission.Fields[field.systemName]);
                    }
            }
        }

        protected String GetFieldStruct(Field field)
        {
            switch (field.fieldType)
            {
                case "image":
                    return
                        String.Format(
                            "<Field Type='URL' DisplayName='{0}' Indexed='FALSE' Format='Hyperlink'  StaticName='{0}' Name='{0}' />",
                            field.systemName);
                default:
                    return
                        String.Format(
                            "<Field Type='Note' DisplayName='{0}' Required='FALSE' EnforceUniqueValues='FALSE'  NumLines='6' RichText='FALSE' StaticName='{0}' Name='{0}' />",
                            field.systemName);
            }

        }

    }
}
